<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@page import="java.awt.Color"%>
<%@page import="java.awt.Font"%>
<%@page import="java.awt.Graphics"%>
<%@page import="java.awt.Graphics2D"%>
<%@page import="java.awt.Image"%>
<%@page import="java.awt.geom.AffineTransform"%>
<%@page import="java.awt.image.BufferedImage"%>
<%@page import="java.io.File"%>
<%@page import="java.io.IOException"%>
<%@page import="java.util.Random"%>
<%@page import="javax.imageio.ImageIO"%>
<%@page import="javax.servlet.ServletException"%>
<%@page import="javax.servlet.http.HttpServlet"%>
<%@page import="javax.servlet.http.HttpServletRequest"%>
<%@page import="javax.servlet.http.HttpServletResponse"%>
<%@page import="javax.servlet.http.HttpSession"%>




<%
//��ֹ����
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "No-cache");
        response.setDateHeader("Expires", 0);
        // ָ����ɵ���Ӧ��ͼƬ
        response.setContentType("image/jpeg");
        int width = 166;        //��֤��ͼƬ�Ŀ��
        int height = 45;        //��֤��ͼƬ�ĸ߶�
        BufferedImage image = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();       //��ȡGraphics��Ķ���
        //��ȡ����ͼƬ
        File bgImgFile = new File(request.getRealPath("css/images/checkCode_bg.jpg"));
        Image src = ImageIO.read(bgImgFile);  //����Image����
        g.drawImage(src, 0, 0, width, height, null);    //���Ʊ���ͼƬ
        /********************************************************/
        Random random = new Random();						//ʵ��һ��Random����
        g.setFont(new Font("����", Font.PLAIN, 16));	//��������
        g.setColor(new Color(255, 255, 255));     //����ѩ������ɫ
        String sRand = "";
        Font mFont = new Font("����", Font.ITALIC, 26);       //ͨ��Font��������
        g.setFont(mFont);   //��������
        g.setColor(new Color(0, 0, 0));     //������ɫΪ��ɫ
        // ���������֤������
        int itmp = 0;
        for (int i = 0; i < 4; i++) {
            if (random.nextInt(2) == 1) {
                itmp = random.nextInt(26) + 65;	//���A~Z����ĸ
            } else {
                itmp = random.nextInt(10) + 48;	//���0~9������
            }
            char ctmp = (char) itmp;
            sRand += String.valueOf(ctmp);
            /****����������ֲ���������תָ���Ƕ�**/
            //��������תָ���Ƕ�
            Graphics2D g2d_word = (Graphics2D) g;
            AffineTransform trans = new AffineTransform();
            //��������
            float scaleSize = random.nextFloat() + 0.5f;
            if (scaleSize < 0.8 || scaleSize > 1.1f) {
                scaleSize = 1f;
            }
            trans.scale(scaleSize, scaleSize);
            g2d_word.setTransform(trans);
            /************************/
            g.drawString(String.valueOf(ctmp), width / 6 * i + 23, random.nextInt(height / 2) + 20);

        }
        // ����ɵ���֤�뱣�浽Session��
        //HttpSession sessionf = request.getSession(true);
        Cookie cookie = new Cookie("randCheckCode",sRand);
        response.addCookie(cookie);
        //session.setAttribute("randCheckCode", sRand);
        g.dispose();
        ImageIO.write(image, "JPEG", response.getOutputStream());
        %>